package com.nl.sevenuser;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import com.nl.redis.RedisCluster;
import com.nl.util.Common;
import com.nl.util.ConfigUtils;
import com.nl.util.ConnectionManager;

public class LoadSend extends Thread {
	private static final Logger logger = Logger.getLogger(LoadSend.class);
	int count1=0;
	public static final String REDISIP;
	public static final String SQL;
	static{		
		REDISIP = ConfigUtils.getInstance().getValue("redisIp");
		SQL = ConfigUtils.getInstance().getValue("sql");
	}
	@Override
	public void run(){
		loadSend();
	}
	private void loadSend(){
		logger.info("*********loadSend start***********");
		long start = System.currentTimeMillis();
		Connection conn = null;
		ResultSet rs = null;
		//		PreparedStatement pres = null;
		Statement stmt = null;
		RedisCluster redisCluster = null;
		try {
			redisCluster = new RedisCluster(REDISIP);
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
//		String sql="select t.REGION_ID,t.receiver,t.mgr_id,to_char(t.create_date, 'yyyy-mm-dd hh24:mi:ss') create_date,t.is_local,t.city_id,a.local_date from boss_sm_send t join sms_template_attr a on t.mgr_id=a.id where t.create_date > to_date('2019/07/10 00:00:00', 'yyyy/mm/dd hh24:mi:ss')";
		try {			
			conn = ConnectionManager.getInstance().getConnection();							
			stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
					ResultSet.CONCUR_READ_ONLY);
			stmt.setFetchSize(300);
			rs = stmt.executeQuery(SQL);
			while(rs.next()){				
				try{
//					logger.info("REGION_ID:"+rs.getString("REGION_ID"));
//					logger.info("receiver:"+rs.getString("receiver"));
//					logger.info("mgr_id:"+rs.getString("mgr_id"));
//					logger.info("create_date:"+rs.getString("create_date"));
//					logger.info("city_id:"+rs.getString("city_id"));
				if (null ==rs.getString("REGION_ID") || "".equals(rs.getString("REGION_ID"))
						|| null ==rs.getString("receiver") || "".equals(rs.getString("receiver"))
						|| null ==rs.getString("mgr_id") || "".equals(rs.getString("mgr_id"))
						|| null ==rs.getString("is_local") || "".equals(rs.getString("is_local"))
						|| null ==rs.getString("create_date") || "".equals(rs.getString("create_date"))
					    || null ==rs.getString("city_id") || "".equals(rs.getString("city_id"))) {
					continue;
				}
				String key =rs.getString("receiver")+"#"+rs.getString("mgr_id");
				Map<String, String> map = redisCluster.hgetAll(key);
				if (null != map && ! map.isEmpty()) {
					continue;
				}
				logger.info("key:"+key+";sendTime"+rs.getString("create_date"));
				count1++;
				Map<String, String> hash =new HashMap<String, String>();				
				hash.put("ARRIVAL_CITY", rs.getString("city_id"));
				hash.put("SEND_TIME", rs.getString("create_date"));
				hash.put("REGION_ID", rs.getString("REGION_ID"));				
				hash.put("IS_LOCAL", rs.getString("is_local"));
				
					if (null !=hash && !hash.isEmpty()) {
						redisCluster.hmset(key, hash);
//						if ( "1".equals(rs.getString("is_local"))) {
//							Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(rs.getString("create_date"));
//							long systime = new Date().getTime();
//							long createDate = date.getTime();
//							int c = (int) ((systime-createDate)/1000);
//							int sec =7*24*60*60 -c;
//							redisCluster.expire(key, sec);
//						} else {
							Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(rs.getString("create_date"));
							long systime = new Date().getTime();
							long createDate = date.getTime();
							int c = (int) ((systime-createDate)/1000);
							int loclTime =  Integer.parseInt(rs.getString("local_date"));
							int sec =loclTime*24*60*60 -c;
							redisCluster.expire(key, sec);
//						}
						if (count1%5000==0) {
							long end = System.currentTimeMillis();
							logger.info("load smsend count :"+count1+",time:"+(end-start));
						}
					}
				
				
				
			}catch (Exception e) {
				logger.error("redis set hash error"+e);
				e.printStackTrace();
			}
			}
			logger.info("load smsend total"+count1);
			Common.sendWarnSms("load smsend total"+count1);
		} catch (Exception e) {
			logger.info("load smsend error" + e);
			e.printStackTrace();
		} finally {
			try {
				if (null != rs) {
					rs.close();
					rs = null;
				}
				if (null != stmt) {
					stmt.close();
				}
			} catch (SQLException e) {

				e.printStackTrace();
			}
			DbUtils.closeQuietly(conn, null, null);
			try {
				redisCluster.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}	
	}
}
